package com.lefevre.cms.service.links.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lefevre.cms.bean.links.Links;
import com.lefevre.cms.mapper.LinksMapper;
import com.lefevre.cms.service.links.LinksService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 友情链接
 *
 * @author Lefevre
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LinksServiceImpl extends ServiceImpl<LinksMapper, Links> implements LinksService {

    /**
     * 根据Id查询友情链接
     *
     * @param linkId 友情链接Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Links findById(Integer linkId) {
        return baseMapper.selectById(linkId);
    }

    /**
     * 查询所有友情链接
     *
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public List<Links> findAllLinks() {
        return baseMapper.selectList(new LambdaQueryWrapper<Links>().orderByDesc(Links::getSort));
    }

    /**
     * 查询所有友情链接 - 缓存
     *
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Cacheable(value = "findAllLinks_cache", key = "'findAllLinks_default'")
    public List<Links> findAllLinks_cache() {
        return this.findAllLinks();
    }


    /**
     * 保存友情链接
     *
     * @param links
     */
    @CacheEvict(value = "findAllLinks_cache", allEntries = true)
    public void saveLinks(Links links) {
        baseMapper.insert(links);
    }


    /**
     * 修改友情链接
     *
     * @param links
     * @return
     */
    @CacheEvict(value = "findAllLinks_cache", allEntries = true)
    public Integer updateLinks(Links links) {
        return baseMapper.deleteById(links);
    }

    /**
     * 删除友情链接
     *
     * @param linkId 友情链接Id
     */
    @CacheEvict(value = "findAllLinks_cache", allEntries = true)
    public Integer deleteLinks(Integer linkId) {
        return baseMapper.deleteById(linkId);
    }

}
